﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using Itop.YHPC.Model;
using System.Data;
using Itop.YHPC.controller;

namespace Itop.YHPC {
    public class BasePage: System.Web.UI.Page {
        /// <summary>
        /// 模块页面代码，用作判断权限
        /// </summary>
        protected virtual string ProgCode {
            get { return null; }
        }
        protected virtual void Page_Load(object sender, EventArgs e) {
            util.CheckLogin();
            if (ProgCode != null) {
                if (!CheckProg(ProgCode)) {

                    HttpContext.Current.Response.Write(string.Format(@"<script>alert('你没有相关模块的权限!');top.location.href='../Default.aspx';</script>"));
                }
            }
        }
       public static string GetLoginID() {
            string loginid="";
            if (HttpContext.Current.Session["user"] != null) {
                loginid = (HttpContext.Current.Session["user"] as sysuser).LoginID;
            }
            return loginid;
        }
       public string GetUserName() {
           string name = "";
           try {
               name = GetUser().UserName;
           } catch { }
           return name;
       }
        public static yhpcOrg GetUserDw(){
           yhpcOrg org= new yhpcOrg();
           org=yhpcOrgController.Bll.Get(GetUser().dwID);
           return org;
        }
       public static sysuser GetUser() {
           util.CheckLogin();
           return (HttpContext.Current.Session["user"] as sysuser);
       }
        /// <summary>
        /// 获取用户模块权限,返回表中包含的字段有(sysprog.*,sysgroupprog.fun1-fun9)
        /// </summary>
        /// <param name="loginid"></param>
        /// <returns></returns>
       public static DataTable GetUserProgs(string loginid) {
           util.CheckLogin();
           if (string.IsNullOrEmpty(loginid))
               loginid = GetLoginID();
           DataTable dt = null;
           IList list = Global.SqlMapper.GetList("SelectProgByUserID", loginid);
           dt = ConvertHelper.HashTablesToDataTable(list);
           if (dt == null) dt = new DataTable();
           return dt;

       }
        /// <summary>
        /// 检查权限
        /// </summary>
        /// <param name="prog">可以是progid,progcode,progname三种值中的一种</param>
        /// <returns>返回真为有权限</returns>
       public static bool CheckProg(string prog) {
#if rabbit
           return true;
#endif
           if (GetUser().LoginID == "admin") return true;
           if (HttpContext.Current.Session["userprog"] == null) {
               HttpContext.Current.Session["userprog"] = GetUserProgs(null);
           }
           DataTable dt = HttpContext.Current.Session["userprog"] as DataTable;
           if (dt == null || dt.Rows.Count == 0) return false;
           DataRow[] rows = dt.Select(string.Format("progid='{0}' or progcode='{1}' or progname='{2}'", prog, prog,prog));

           return rows == null ? false : rows.Length > 0;
       }
    }
}
